const path = require('path');
const requireContext = require('require-context');
const Router = require('koa-router');
const router = new Router();

const addRouter = target => {
  target.routers.forEach(item => {
    router[item.method](target.prefix + (item.url ? item.url : ''), item.handler);
  })
}

const modulesFiles = requireContext(path.join(__dirname + '/modules'), true, /\.js$/);
modulesFiles.keys().forEach((modules, modulePath) => {
  addRouter(modulesFiles(modules));
})
module.exports = router;